package com.xcfw.dorm.controller.dormassistant;

import com.xcfw.dorm.controller.BaseController;
import com.xcfw.dorm.entity.Leave;
import com.xcfw.dorm.entity.NumEnum;
import com.xcfw.dorm.entity.ReturnJson;
import com.xcfw.dorm.exception.ServiceException;
import com.xcfw.dorm.exception.ValidationException;
import com.xcfw.dorm.seviceimpl.dormassistant.LeaveServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

/**
 * @program: Dorm
 * @description: 请假
 * @author: Bruse Queen
 * @create: 2018-04-07 16:42
 **/
@RestController
@Slf4j
@RequestMapping(value = "/dormAssistant")
@CrossOrigin
public class LeaveController extends BaseController {
    @Autowired
    @Qualifier("leaveServiceImpl")
    private LeaveServiceImpl zsgcService;

    @RequestMapping(value = "/addLeave", method = RequestMethod.POST)
    public ReturnJson addLeave(@RequestBody Leave leave) {
        try {
            zsgcService.add(leave);
            result = new ReturnJson(NumEnum.SUCCESS.getIndex(), "请假信息新增成功！");
        } catch (ValidationException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (ServiceException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (Exception e) {
            log.error("LeaveController---addLeave--error", e);
            result = new ReturnJson(NumEnum.FAIL.getIndex(), "数据异常,请稍后再试！");
        }
        return result;
    }

    @RequestMapping(value = "/deleteLeave", method = RequestMethod.POST)
    public ReturnJson deleteLeave(@RequestBody Leave leave) {
        try {
            zsgcService.delete(leave.getId());
            result = new ReturnJson(NumEnum.SUCCESS.getIndex(), "请假信息删除成功！");
        } catch (ValidationException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (ServiceException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (Exception e) {
            log.error("LeaveController---deleteLeave--error", e);
            result = new ReturnJson(NumEnum.FAIL.getIndex(), "数据异常,请稍后再试！");
        }
        return result;
    }

    @RequestMapping(value = "/updateLeave", method = RequestMethod.POST)
    public ReturnJson updateLeave(@RequestBody Leave leave) {
        try {
            zsgcService.update(leave);
            result = new ReturnJson(NumEnum.SUCCESS.getIndex(), "请假信息修改成功！");
        } catch (ValidationException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (ServiceException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (Exception e) {
            log.error("LeaveController---updateLeave--error", e);
            result = new ReturnJson(NumEnum.FAIL.getIndex(), "数据异常,请稍后再试！");
        }
        return result;
    }

    @RequestMapping(value = "/selectLeave", method = RequestMethod.POST)
    public ReturnJson selectLeave(@RequestBody Leave leave) {
        try {
            result = (ReturnJson) zsgcService.select(leave);
        } catch (ValidationException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (ServiceException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (Exception e) {
            log.error("LeaveController---selectLeave--error", e);
            result = new ReturnJson(NumEnum.FAIL.getIndex(), "数据异常,请稍后再试！");
        }
        return result;
    }
}
